<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Hello React</title>
</head>
<body>

  <div id="root"></div>

  <script crossorigin src="../lib/react.js"></script>
  <script crossorigin src="../lib/react-dom.js"></script>
  <script src="../lib/babel.js"></script>
  
  <script type="text/babel">
    
    class App extends React.Component {
      
      constructor() {
        super()
        this.state = {
          movies: ['千年女优', '红辣椒', '未麻的部屋'],
          currentIndex: 0
        }
      }
      handleClick(index) {
        this.setState({
          currentIndex: index
        })
      }

      render() {
        const { movies, currentIndex } = this.state
        const liEls = movies.map((item, index) => {
          return (
            <li 
              key={index} 
              className={ currentIndex == index ? 'active': ''}
              onClick={() => this.handleClick(index)}
            >
              {item}
            </li>
          )
        })
        return (
          <div>
            <h2>电影列表</h2>
            <ul>           
              { liEls }
            </ul>
          </div>
        )
      }
    }

    // 将组件渲染到页面
    const root = ReactDOM.createRoot(document.querySelector("#root"))
    root.render(<App />)
  </script>

  <style>
    .active {
      color: red;
    }
  </style>
</body>
</html>